<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'sh:order:bx:page'"
        :excelPer="'sh:order:bx:export'"
        @search="search"
        @handleDataExport="handleDataExport"
        :showSearchButton="true"
        :showExportButton="true"
        @export="excelInfo"
      >
        <el-date-picker
          v-model="form.createTime"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-select
          clearable filterable
          style="width: 200px"
          v-model="form.carBrandId"
          placeholder="请选择品牌"
          @change="handleCarBrand"
        >
          <el-option
            v-for="item in allBrand"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-select
          clearable filterable
          style="width: 200px"
          v-model="form.carSeriesId"
          placeholder="请选择车系"
        >
          <el-option
            v-for="item in allSeries"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-input
          v-model="form.orderno"
          clearable
          placeholder="订单号"
          style="width: 200px"
        />
        <el-input
          v-model="form.client"
          clearable
          placeholder="客户名称/手机号"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-money"
            title="确认收款"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'sh:order:bx:proceeds'"
            @click="proceedsOk(row)"
          ></i>
          <i
            class="el-icon-edit"
            title="补充信息"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'sh:order:bx:supplementary'"
            @click="updateText(row)"
          ></i>
        </template>
      </data-table>
      <el-dialog
        title="补充信息"
        :visible.sync="visible"
        width="40%">
        <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
          <el-row>
            <el-col :span="12">
              <el-form-item label="商业保险号:" prop="businessNo">
                <el-input v-model="addForm.businessNo" clearable placeholder="请输入商业保险号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="交强险单号:" prop="insuranceNo">
                <el-input v-model="addForm.insuranceNo" clearable placeholder="请输入交强险单号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投保日期:" prop="insureTime">
                <el-date-picker
                  style="width: 100%"
                  v-model="addForm.insureTime"
                  type="date"
                  placeholder="投保日期"
                  clearable
                  value-format="yyyy-MM-dd hh:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投保总公司:" prop="headOffice">
                <el-input v-model="addForm.headOffice" clearable placeholder="请输入投保总公司"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分公司:" prop="branchOffice">
                <el-input v-model="addForm.branchOffice" clearable placeholder="请输入分公司"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="保单类型:" prop="guaranteeSlipType">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.guaranteeSlipType"
                  placeholder="保单类型"
                >
                  <el-option
                    v-for="item in getDict(['保单类型'])['保单类型']"
                    :key="item.index"
                    :value="item.value"
                    :label="item.text"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="保险起期:" prop="insuranceStartTime">
                <el-date-picker
                  style="width: 100%"
                  v-model="addForm.insuranceStartTime"
                  type="date"
                  placeholder="保险起期"
                  clearable
                  value-format="yyyy-MM-dd hh:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="保险止期:" prop="insuranceOverTime">
                <el-date-picker
                  style="width: 100%"
                  v-model="addForm.insuranceOverTime"
                  type="date"
                  placeholder="保险止期"
                  clearable
                  value-format="yyyy-MM-dd hh:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="保险业务员:" prop="salesman">
                <el-input v-model="addForm.salesman" clearable placeholder="请输入保险业务员"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="保险门店:" prop="insuranceShop">
                <el-input v-model="addForm.insuranceShop" clearable placeholder="请输入保险门店"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
         <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  getList,
  updateData, proceedsOk
} from '@/api/pre-sale/orderManagement/insurance'
import { getDict, exportExcel } from '@/utils/index'
import { getAllBrand, getSeries } from '@/api/after-sale/systemManage/carModel'

export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: getList,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.payState = data.payState === 1 ? '已支付' : '未支付'
          data.guaranteeSlipType = data.guaranteeSlipType === 1 ? '新保' : '续保'
          return data
        })
      }
    })
  ],

  data() {
    return {
      allBrand: [],
      allSeries: [],
      visible: false,
      addForm: {},
      addFormRules: {},
      getDict: getDict,
      form: {
        client: '' // 客户信息
      },
      tableColumnAttributes: [
        {
          name: '订单号',
          value: 'orderno',
          type: 'text'
        },
        {
          name: '所属门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '品牌',
          value: 'carBrandName',
          type: 'text'
        },
        {
          name: '车型',
          value: 'carModelName',
          type: 'text'
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text'
        },
        {
          name: '客户姓名',
          value: 'clientName',
          type: 'text'
        },
        {
          name: '电话',
          value: 'clientPhone',
          type: 'text'
        },
        {
          name: '商业险单号',
          value: 'businessNo',
          type: 'text'
        },
        {
          name: '交强险单号',
          value: 'insuranceNo',
          type: 'text'
        },
        {
          name: '投保日期',
          value: 'insureTime',
          type: 'text'
        },
        {
          name: '保险总公司',
          value: 'headOffice',
          type: 'text'
        },
        {
          name: '保险分公司',
          value: 'branchOffice',
          type: 'text'
        },
        {
          name: '保单类型',
          value: 'guaranteeSlipType',
          type: 'text'
        },
        {
          name: '交强险',
          value: 'jqx',
          type: 'text'
        },
        {
          name: '商业险',
          value: 'syx',
          type: 'text'
        },
        {
          name: '合计保费',
          value: 'totalInsuranceAmount',
          type: 'text'
        },
        {
          name: '支付状态',
          value: 'payState',
          type: 'text'
        },
        {
          name: '保险佣金',
          value: 'bxyj',
          type: 'text'
        },
        {
          name: '保险起期',
          value: 'insuranceStartTime',
          type: 'text'
        },
        {
          name: '保险止期',
          value: 'insuranceOverTime',
          type: 'text'
        },
        {
          name: '保险门店',
          value: 'insuranceShop',
          type: 'text'
        },
        {
          name: '保险业务员',
          value: 'salesman',
          type: 'text'
        },
        {
          name: '制单人',
          value: 'createUser',
          type: 'text'
        },
        {
          name: '审核状态',
          value: 'auditStatus',
          type: 'dict',
          dict: '审核状态'
        }
      ],
      dict: {}
    }
  },
  methods: {
    async handleCarBrand(val) {
      val && await getSeries(val).then((res) => {
        const { success, data } = res
        if (success) this.allSeries = data
      })
    },
    // 查询
    search(form) {
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
    handleDataExport() {

    },
    // 补充信息
    updateText(row) {
      let r=Object.assign({},row)
      r.guaranteeSlipType = r.guaranteeSlipType === '新保' ? 1 : 2
      this.visible = true
      this.$nextTick(() => {
        this.addForm = {
          ...this.addForm,
          ...r
        }
      })
    },
    async proceedsOk(row) {
      this.$confirm('是否确认收款操作', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const { success } = await proceedsOk(row.id)
          if (success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.setTableData()
          } else {
            this.$message.error('操作失败')
          }
        })
    },
    // 取消
    handleClose() {
      this.$refs.addForm.resetFields()
      this.visible = false
    },
    // 确认
    handleOk() {
      this.$confirm('是否确认收款操作', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$refs.addForm.validate((valid) => {
            if (valid) {
              this.updateData()
            }
          })
        })
    },
    // 补充信息接口
    async updateData() {
      console.log(this.addForm)
      await updateData({
        ...this.addForm
      }).then((res) => {
        const { success } = res
        if (success) this.setTableData()
        this.visible = false
      })
    },
    // 导出
    excelInfo() {
      exportExcel('BxOrder', '保险订单', { ...this.form }, this.pageSize, this.currentPage)
    }
  },
  async mounted() {
    await getAllBrand().then((res) => {
      const { success, data } = res
      if (success) this.allBrand = data
    })
  }
}
</script>
